<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title th:text="${title}">泸州市实验小学城西学校视频导航页</title>
    <link rel="shortcut icon" th:href="@{../ico/favicon.ico}"/>

    <!-- Bootstrap 5 CSS -->
    <link th:href="@{css/bootstrap.css}" rel="stylesheet">
    <!-- Font Awesome 图标 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;500;700&family=Noto+Serif+SC:wght@400;600&display=swap" rel="stylesheet">
    <!-- Animate.css -->
    <link rel="stylesheet" th:href="@{css/animate.css}">

    <style>
        :root {
            --primary-color: #1e88e5; /* 主色调 - 蓝色 */
            --primary-light: #6ab7ff;
            --primary-dark: #005cb2;
            --secondary-color: #ff9800; /* 辅助色 - 橙色 */
            --accent-color: #f44336; /* 强调色 - 红色 */
            --light-bg: #f5f7fa;
            --dark-text: #263238;
            --light-text: #eceff1;
            --gray-text: #607d8b;
            --border-radius: 12px;
            --box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            --box-shadow-hover: 0 8px 30px rgba(0, 0, 0, 0.12);
            --transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
        }

        body {
            font-family: 'Noto Sans SC', sans-serif;
            background-color: var(--light-bg);
            color: var(--dark-text);
            line-height: 1.6;
        }

        /* 导航栏样式 - 重新设计 */
        .navbar {
            background: white;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            padding: 15px 0;
            transition: var(--transition);
        }

        .navbar.scrolled {
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
        }

        .navbar-brand {
            font-weight: 700;
            font-size: 1.8rem;
            color: var(--primary-dark) !important;
            display: flex;
            align-items: center;
            font-family: 'Noto Serif SC', serif;
        }

        .navbar-brand i {
            margin-right: 10px;
            font-size: 1.5rem;
            color: var(--secondary-color);
        }

        /* 分类导航 - 侧边栏设计 */
        .category-sidebar {
            position: sticky;
            top: 100px;
            height: calc(100vh - 120px);
            overflow-y: auto;
            padding-right: 15px;
            scrollbar-width: thin;
        }

        .category-sidebar::-webkit-scrollbar {
            width: 6px;
        }

        .category-sidebar::-webkit-scrollbar-thumb {
            background-color: rgba(0, 0, 0, 0.2);
            border-radius: 3px;
        }

        .category-header {
            font-weight: 600;
            font-size: 1.1rem;
            color: var(--primary-dark);
            padding: 10px 15px;
            margin-bottom: 10px;
            border-left: 4px solid var(--primary-color);
            background-color: rgba(30, 136, 229, 0.05);
        }

        .category-item {
            padding: 8px 15px;
            margin-bottom: 5px;
            border-radius: var(--border-radius);
            cursor: pointer;
            transition: var(--transition);
            position: relative;
        }

        .category-item:hover {
            background-color: rgba(30, 136, 229, 0.1);
        }

        .category-item.active {
            background-color: rgba(30, 136, 229, 0.15);
            font-weight: 500;
            color: var(--primary-dark);
        }

        .category-item.active::before {
            content: '';
            position: absolute;
            left: 0;
            top: 0;
            height: 100%;
            width: 3px;
            background-color: var(--primary-color);
            border-radius: 0 3px 3px 0;
        }

        .subcategory-list {
            padding-left: 20px;
            margin-top: 5px;
            display: none;
        }

        .subcategory-item {
            padding: 6px 10px;
            margin-bottom: 3px;
            border-radius: 6px;
            font-size: 0.95rem;
            transition: var(--transition);
        }

        .subcategory-item:hover, .subcategory-item.active {
            background-color: rgba(30, 136, 229, 0.1);
            color: var(--primary-dark);
        }

        /* 搜索框样式 - 重新设计 */
        .search-container {
            position: relative;
            width: 100%;
            max-width: 400px;
        }

        .search-input {
            border-radius: 50px;
            padding-left: 45px;
            border: 1px solid #e0e0e0;
            background-color: white;
            color: var(--dark-text);
            height: 45px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
            transition: var(--transition);
        }

        .search-input:focus {
            border-color: var(--primary-light);
            box-shadow: 0 4px 12px rgba(30, 136, 229, 0.2);
        }

        .search-icon {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: var(--gray-text);
            z-index: 10;
        }

        .search-btn {
            border-radius: 50px;
            padding: 0 20px;
            height: 45px;
            background-color: var(--primary-color);
            border: none;
            transition: var(--transition);
        }

        .search-btn:hover {
            background-color: var(--primary-dark);
        }

        /* 主要内容区域 */
        .main-container {
            padding: 30px 0 50px;
        }

        .page-header {
            margin-bottom: 30px;
            position: relative;
        }

        .category-title {
            font-size: 2rem;
            font-weight: 600;
            color: var(--primary-dark);
            margin-bottom: 15px;
            font-family: 'Noto Serif SC', serif;
            position: relative;
            display: inline-block;
        }

        .category-title::after {
            content: '';
            position: absolute;
            bottom: -5px;
            left: 0;
            width: 60px;
            height: 3px;
            background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
            border-radius: 3px;
        }

        .category-description {
            color: var(--gray-text);
            margin-bottom: 20px;
            max-width: 800px;
        }

        /* 视频卡片样式 - 重新设计 */
        .video-card {
            border: none;
            border-radius: var(--border-radius);
            overflow: hidden;
            transition: var(--transition);
            box-shadow: var(--box-shadow);
            margin-bottom: 25px;
            background-color: white;
            position: relative;
        }

        .video-card:hover {
            transform: translateY(-5px);
            box-shadow: var(--box-shadow-hover);
        }

        .video-card.external::after {
            content: '外部链接';
            position: absolute;
            top: 10px;
            right: 10px;
            background-color: var(--secondary-color);
            color: white;
            font-size: 0.7rem;
            padding: 2px 8px;
            border-radius: 10px;
            z-index: 2;
        }

        .video-thumbnail {
            position: relative;
            overflow: hidden;
            padding-top: 56.25%; /* 16:9 宽高比 */
        }

        .video-thumbnail img {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: transform 0.5s ease;
        }

        .video-card:hover .video-thumbnail img {
            transform: scale(1.05);
        }

        .video-thumbnail::after {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(to top, rgba(0, 0, 0, 0.5), transparent 40%);
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .video-card:hover .video-thumbnail::after {
            opacity: 1;
        }

        .play-icon {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 60px;
            height: 60px;
            background-color: rgba(255, 255, 255, 0.9);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            opacity: 0;
            transition: var(--transition);
            z-index: 2;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
        }

        .play-icon i {
            color: var(--primary-color);
            font-size: 1.8rem;
            margin-left: 3px;
        }

        .video-card:hover .play-icon {
            opacity: 1;
        }

        .video-info {
            padding: 15px;
        }

        .video-title {
            font-weight: 500;
            margin-bottom: 5px;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
            color: var(--dark-text);
            min-height: 48px; /* 保持标题高度一致 */
        }

        /* 无结果提示 */
        .no-results {
            text-align: center;
            padding: 50px 0;
            color: var(--gray-text);
        }

        .no-results i {
            font-size: 3rem;
            color: var(--primary-light);
            margin-bottom: 20px;
        }

        /* 页脚样式 - 重新设计 */
        .footer {
            background-color: var(--dark-text);
            color: var(--light-text);
            padding: 50px 0 20px;
            position: relative;
        }

        .footer::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 10px;
            background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
        }

        .footer-logo {
            font-size: 1.5rem;
            font-weight: 700;
            margin-bottom: 15px;
            font-family: 'Noto Serif SC', serif;
        }

        .footer-logo i {
            color: var(--secondary-color);
            margin-right: 10px;
        }

        .footer-about {
            max-width: 300px;
        }

        .footer-links h5 {
            font-size: 1.2rem;
            margin-bottom: 20px;
            position: relative;
            padding-bottom: 10px;
            font-family: 'Noto Serif SC', serif;
        }

        .footer-links h5::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 40px;
            height: 2px;
            background-color: var(--secondary-color);
        }

        .footer-links ul {
            list-style: none;
            padding-left: 0;
        }

        .footer-links li {
            margin-bottom: 10px;
        }

        .footer-links a {
            color: rgba(255, 255, 255, 0.7);
            text-decoration: none;
            transition: color 0.3s;
            display: inline-block;
            padding: 3px 0;
        }

        .footer-links a:hover {
            color: white;
            transform: translateX(5px);
        }

        .contact-info i {
            width: 20px;
            text-align: center;
            margin-right: 10px;
            color: var(--secondary-color);
        }

        .social-icons {
            margin-top: 20px;
        }

        .social-icons a {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 36px;
            height: 36px;
            background-color: rgba(255, 255, 255, 0.1);
            border-radius: 50%;
            color: white;
            margin-right: 10px;
            transition: var(--transition);
        }

        .social-icons a:hover {
            background-color: var(--secondary-color);
            transform: translateY(-3px);
        }

        .copyright {
            border-top: 1px solid rgba(255, 255, 255, 0.1);
            padding-top: 20px;
            margin-top: 30px;
            font-size: 0.9rem;
            color: rgba(255, 255, 255, 0.5);
            text-align: center;
        }

        /* 响应式调整 */
        @media (max-width: 1199.98px) {
            .category-sidebar {
                height: auto;
                max-height: 400px;
                margin-bottom: 30px;
                position: static;
            }
        }

        @media (max-width: 991.98px) {
            .navbar-brand {
                font-size: 1.5rem;
            }

            .category-title {
                font-size: 1.8rem;
            }
        }

        @media (max-width: 767.98px) {
            .category-title {
                font-size: 1.6rem;
            }

            .video-card {
                margin-bottom: 20px;
            }
        }

        @media (max-width: 575.98px) {
            .navbar-brand {
                font-size: 1.3rem;
            }

            .category-title {
                font-size: 1.4rem;
            }

            .footer-links {
                margin-bottom: 30px;
            }
        }

        /* 动画效果 */
        .animate-fade-in {
            animation: fadeIn 0.6s ease forwards;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .video-card {
            opacity: 0;
        }

        /* 骨架屏加载效果 */
        .skeleton {
            background-color: #e0e0e0;
            border-radius: 4px;
            overflow: hidden;
            position: relative;
        }

        .skeleton::after {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.5), transparent);
            animation: shimmer 1.5s infinite;
        }

        @keyframes shimmer {
            0% { transform: translateX(-100%); }
            100% { transform: translateX(100%); }
        }

        /* 分类导航折叠/展开动画 */
        .rotate-icon {
            transition: transform 0.3s ease;
        }

        .rotate-icon.rotated {
            transform: rotate(90deg);
        }
    </style>
</head>
<body>
<!-- 导航栏 -->
<nav class="navbar navbar-expand-lg navbar-light sticky-top">
    <div class="container">
        <a class="navbar-brand">
            <i class="fas fa-graduation-cap"></i>
            <span th:text="${title}">泸州市实验小学城西学校视频导航页</span>
        </a >

        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarContent">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarContent">
            <!-- 搜索框移动到导航栏右侧 -->
            <form id="searchForm" class="d-flex ms-auto search-container">
                <i class="fas fa-search search-icon"></i>
                <input class="form-control search-input" type="search" placeholder="搜索视频..."
                       aria-label="Search" id="searchInput">
                <button class="btn btn-primary ms-2 search-btn" type="submit">
                    <i class="fas fa-search"></i>
                </button>
            </form>
        </div>
    </div>
</nav>

<!-- 主要内容 -->
<div class="main-container">
    <div class="container">
        <div class="row">
            <!-- 分类侧边栏 -->
            <div class="col-lg-3">
                <div class="category-sidebar card shadow-sm">
                    <div class="card-body">
                        <h5 class="category-header">视频分类</h5>

                        <!-- 动态生成分类树 -->
                        <div id="categoryTree">
                            <div class="category-item" th:each="category1 : ${categories_level_1}">
                                <div class="d-flex justify-content-between align-items-center"
                                     th:attr="data-category-id=${category1.id}">
                                    <span th:text="${category1.name}">一级分类</span>
                                    <i class="fas fa-chevron-right rotate-icon" style="font-size: 0.8rem;"></i>
                                </div>
                                <ul class="subcategory-list">
                                    <li th:each="category2 : ${category1.subcategories}">
                                        <a class="subcategory-item d-block"
                                           th:href="@{/?(subcategory_id=${category2.id})}"
                                           th:text="${category2.name}"
                                           th:classappend="${category2.id == currentSubcategoryId} ? 'active'">
                                            二级分类
                                        </a >
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 视频内容区域 -->
            <div class="col-lg-9">
                <div class="page-header">
                    <h1 class="category-title" th:text="${current_subcategory_name}">当前分类</h1>
                </div>

                <div class="row" id="videoCardsContainer">
                    <!-- 动态显示视频卡片 -->
                    <div class="col-xl-3 col-lg-4 col-md-6" th:each="video : ${videos}">
                        <div class="video-card animate-fade-in"
                             th:classappend="${#strings.startsWith(video.url, 'http')} ? 'external'">
                            <div class="video-thumbnail">
                                <!-- 如果 video.url 是 HTTP(S) 链接，则直接创建一个普通链接 -->
                                <th:block th:if="${#strings.startsWith(video.url, 'http')}">
                                    <a href="#" th:href="@{${video.url}}" target="_blank" class="stretched-link">
                                        <img th:src="@{/images/{imageName}(imageName=${video.image})}" alt="视频封面" loading="lazy">
                                        <div class="play-icon">
                                            <i class="fas fa-play"></i>
                                        </div>
                                    </a >
                                </th:block>

                                <!-- 否则，创建指向播放页面的链接 -->
                                <th:block th:unless="${#strings.startsWith(video.url, 'http')}">
                                    <a href="#" th:href="@{/playVideo(videoUrl=${T(java.net.URLEncoder).encode(video.url, 'UTF-8')})}"
                                       target="_blank" class="stretched-link">
                                        <img th:src="@{/images/{imageName}(imageName=${video.image})}" alt="视频封面" loading="lazy">
                                        <div class="play-icon">
                                            <i class="fas fa-play"></i>
                                        </div>
                                    </a >
                                </th:block>
                            </div>
                            <div class="video-info">
                                <h5 class="video-title" th:text="${video.title}">视频标题</h5>
                            </div>
                        </div>
                    </div>

                    <!-- 无结果提示 -->
                    <div class="col-12" th:if="${videos != null && videos.empty}">
                        <div class="no-results">
                            <i class="fas fa-video-slash"></i>
                            <h4>暂无相关视频</h4>
                            <p>当前分类下还没有视频内容，请尝试其他分类或搜索关键词</p >
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Bootstrap Bundle with Popper -->
<script th:src="@{js/jQuery.js}"></script>
<script th:src="@{js/bootstrap.js}"></script>

<script th:inline="javascript">
    $(document).ready(function () {
        // 导航栏滚动效果
        $(window).scroll(function() {
            if ($(this).scrollTop() > 50) {
                $('.navbar').addClass('scrolled');
            } else {
                $('.navbar').removeClass('scrolled');
            }
        });

        // 分类导航折叠/展开功能
        $('#categoryTree .category-item > div').click(function(e) {
            // 阻止事件冒泡，避免与子分类链接冲突
            if ($(e.target).is('a')) return;

            const $this = $(this);
            const $subList = $this.next('.subcategory-list');
            const $icon = $this.find('.rotate-icon');

            $icon.toggleClass('rotated');
            $subList.slideToggle(200);
        });

        // 默认展开当前选中的分类
        const currentCategoryId = [[${currentSubcategoryId}]];
        if (currentCategoryId) {
            const $activeSubItem = $('.subcategory-item.active');
            if ($activeSubItem.length) {
                $activeSubItem.closest('.subcategory-list').show();
                $activeSubItem.closest('.category-item').find('.rotate-icon').addClass('rotated');
            }
        }

        // 当表单提交时触发搜索功能
        $('#searchForm').on('submit', function (event) {
            event.preventDefault();  // 阻止表单默认提交行为

            const query = $('#searchInput').val().trim();

            if (query.length >= 1) { // 只有当输入至少一个字符时才进行搜索
                searchVideos(query);
            } else {
                // 如果搜索框为空，则可以加载默认视频列表或者清空结果
                searchVideos();
            }
        });

        // 搜索视频的函数
        function searchVideos(query) {
            const params = {
                subcategory_id: "",
                query: query
            };
            window.location.href = buildUrl('/', params);
        }

        // 视频卡片悬停效果
        $('.video-card').hover(
            function() {
                $(this).find('.play-icon').css('opacity', '1');
            },
            function() {
                $(this).find('.play-icon').css('opacity', '0');
            }
        );

        // 骨架屏加载效果
        function showSkeletonLoading() {
            const $container = $('#videoCardsContainer');
            $container.empty();

            for (let i = 0; i < 8; i++) {
                $container.append(`
                    <div class="col-xl-3 col-lg-4 col-md-6">
                        <div class="video-card">
                            <div class="video-thumbnail skeleton" style="height: 0; padding-top: 56.25%;"></div>
                            <div class="video-info">
                                <div class="skeleton" style="height: 20px; width: 80%; margin-bottom: 10px;"></div>
                                <div class="skeleton" style="height: 16px; width: 60%;"></div>
                            </div>
                        </div>
                    </div>
                `);
            }
        }

        // 模拟加载效果 - 实际使用时可以替换为真实数据加载
        if ([[${videos == null}]]) {
            showSkeletonLoading();

            // 模拟异步加载
            setTimeout(function() {
                // 这里应该是实际的数据加载逻辑
                // 加载完成后移除骨架屏，显示实际内容
            }, 1500);
        }
    });

    function buildUrl(basePath, params) {
        // 创建 URLSearchParams 对象用于处理查询参数
        const urlParams = new URLSearchParams();

        // 遍历提供的参数并添加到 URLSearchParams
        for (const [key, value] of Object.entries(params)) {
            if (value !== undefined && value !== null) {
                urlParams.append(key, value);
            }
        }

        // 如果有查询参数，则将它们附加到基础路径后面
        return basePath + (urlParams.toString() ? '?' + urlParams.toString() : '');
    }

    // 解码URL链接
    document.addEventListener("DOMContentLoaded", function () {
        var links = document.querySelectorAll('a.stretched-link');

        for (var i = 0; i < links.length; i++) {
            var link = links[i];
            try {
                var encodedHref = link.getAttribute('href');
                if (encodedHref) {
                    var decodedHref = decodeURIComponent(encodedHref);
                    link.setAttribute('href', decodedHref);
                }
            } catch (e) {
                console.error('解码链接失败:', e);
            }
        }
    });
</script>
</body>
</html>